package com.lijie.bs_recruit.controller;

import com.lijie.bs_recruit.Util.JwtUtil;
import com.lijie.bs_recruit.Util.loginRegister;
import com.lijie.bs_recruit.VO.ResultVO;
import com.lijie.bs_recruit.model.Hr;
import com.lijie.bs_recruit.model.User;
import com.lijie.bs_recruit.service.HrService;
import com.lijie.bs_recruit.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/LoginRegister")
public class LoginRegisterController {
    @Autowired
    HrService hrService;
    @Autowired
    UserService userService;

    //hr user登录
    @RequestMapping("/login")
    public ResultVO login(@RequestBody loginRegister lg){
        Map<String, Object> login=new HashMap<>();
        ResultVO<Object> resultVO = new ResultVO();
        Object person=new Object();
        if(lg!=null){
            if("求职者".equals(lg.getType())){//求职者登录
                login = userService.login(lg.getUsername(), lg.getPassword());
                person=login.get("user");
                User user=(User)person;
                if(user!=null){
                    String token = JwtUtil.sign(user.getUserName());
                    resultVO.setToken(token);
                }
                resultVO.setMessage("user");
            }else if("招聘者".equals(lg.getType())){//HR登录
                login = hrService.login(lg.getUsername(), lg.getPassword());
                person=login.get("hr");
                Hr hr=(Hr)person;
                if(hr!=null){
                    String token = JwtUtil.sign(hr.getHruserName());
                    resultVO.setToken(token);
                }
                resultVO.setMessage("hr");
            }
        }
        Integer status = (Integer) login.get("status");
        if(status!=null) {
            resultVO.setStatus(status);
            if(status==0){
                //登录成功
                resultVO.setObject(person);
            }
        }
        return resultVO;
    }

    //user注册
    @RequestMapping("/userRegister")
    public String userRegister(@RequestBody User user){
        String result="";
        User userByUsername = userService.getUserByUsername(user.getUserName());
        if(userByUsername !=null){
            result= "已存在该用户";
        }else{
            boolean save = userService.save(user);
            if(save){
                result= "succ";
            }
        }
        return result;
    }
    //Hr注册
    @RequestMapping("/hrRegister")
    public String hrRegister(@RequestBody Hr hr){
        String result="";
        Hr hrByhrsername = hrService.getHrByhrsername(hr.getHruserName());
        if(hrByhrsername !=null){
            result= "已存在该用户";
        }else{
            boolean save = hrService.save(hr);
            if(save){
                result= "succ";
            }
        }
        return result;
    }
}
